﻿//初始化表单
function initialServiceInfo() {
    //绑定下拉列表
    bindSelectList();

    //根据action参数判断是添加、编辑、查看
    var name, value;
    var urlinfo = window.location.href; //获取当前页面的url
    var num = urlinfo.indexOf("?");
    urlinfo = urlinfo.substr(num + 1);
    var action = ""; //页面URL中读取action参数值 -待填

    var arr = urlinfo.split("&");
    for (var i = 0; i < arr.length; i++) {
        num = arr[i].indexOf("=");
        if (num > 0) {
            name = arr[i].substr(0, num);
            value = arr[i].substr(num + 1);
            this[name] = value;
            if (name == "action")
                action = value;
        }
    }

    if (action == "edit" || action == "view") {
        $.ajax({
            type: "post",
            async: false,
            url: "/fuwu.ashx",
            data: //发送到服务器的参数
            {
            stype: "getServiceInfo",
            sid: sid
        },
        datatype: "json",
        success: function (result) {
            //进行表单文本项值绑定-待填
            var pinfo = JSON.parse(result);
            var fieldText = ["xuhao", "bianhao", "URL", "servicename", "servicekeyword", "zuojing", "zuowei",
                    "youjing", "youwei", "zhuceren", "zhuceshijian", "jianjie", "beizhu", "jigou", "dizhi", "lianxiren", "lianxifangshi"
                ]; //text id
            var fieldSql = ["S_XH", "S_BH", "S_URL", "S_NAME", "S_GJC", "S_ZSJ", "S_ZSW", "S_YXJ", "S_YXW",
                    "S_ZCR", "S_ZCSJ", "S_JJ", "S_BZ", "S_JG", "S_DZ", "S_LXR", "S_LXFS"
                ]; //对应的数据库字段ID
            for (var k = 0; k < fieldText.length; k++) {
                $("#" + fieldText[k]).attr("value", pinfo[0][fieldSql[k]]);
            }

            //进行下拉列表选中项设置-待填
            var selectList = ["leixing", "zhuti", "quyu", "zuobiao", "touying", "quanxian", "zhouqi"]; //select ID
            var selectListSql = ["S_TYPE", "S_ZT", "S_QY", "S_ZBX", "S_TY", "S_QX", "S_ZQ"]; //对应数据库字段ID
            for (var j = 0; j < selectList.length; j++) {
                $("#" + selectList[j]).val(pinfo[0][selectListSql[j]]);
            }
        }
    });
    if (action == "view") {
        //隐藏提交按钮-待填
        $("#btSubmit").hide();
    }
}
    else {
        $.ajax({
            type: "post",
            async: false,
            url: "/fuwu.ashx",
            data: //发送到服务器的参数
            {
            stype: "getmaxxh"
            
        },
        datatype: "json",
        success: function (result) {
            document.getElementById("xuhao").value=result;
        }
    });
    }
}

//提交
function release() {
    var str1 = document.getElementById("xuhao").value;
    var str2 = document.getElementById("bianhao").value;
    var str3 = document.getElementById("URL").value;
    var str4 = document.getElementById("servicename").value;
    var str5 = document.getElementById("servicekeyword").value;
    var str6 = document.getElementById("zuojing").value;
    var str7 = document.getElementById("zuowei").value;
    var str8 = document.getElementById("youjing").value;
    var str9 = document.getElementById("youwei").value;
    var str10 = document.getElementById("leixing").value;
    var str11 = document.getElementById("zhuti").value;
    var str12 = document.getElementById("quyu").value;
    var str13 = document.getElementById("zuobiao").value;
    var str14 = document.getElementById("touying").value;
    var str15 = document.getElementById("quanxian").value;
    var str16 = document.getElementById("zhouqi").value;
    var str17 = document.getElementById("zhuceren").value;
    var str18 = document.getElementById("zhuceshijian").value;
    //var str18 = Date.toLocaleString();
    var str19 = document.getElementById("jianjie").value;
    var str20 = document.getElementById("beizhu").value;
    var str21 = document.getElementById("jigou").value;
    var str22 = document.getElementById("dizhi").value;
    var str23 = document.getElementById("lianxiren").value;
    var str24 = document.getElementById("lianxifangshi").value;
    var action = ""; //页面URL中读取action参数值 -待填
    var sid = "";
    var name, value;
    var urlinfo = window.location.href; //获取当前页面的url
    var num = urlinfo.indexOf("?");
    urlinfo = urlinfo.substr(num + 1);
    var arr = urlinfo.split("&");
    for (var i = 0; i < arr.length; i++) {
        num = arr[i].indexOf("=");
        if (num > 0) {
            name = arr[i].substr(0, num);
            value = arr[i].substr(num + 1);
            this[name] = value;
            if (name == "action")
                action = value;
            else if (name == "sid")
                sid = value;
        }
    }


    ///var sid = document.getElementById("sid").value; ; //页面URL中读取sid参数值-待填
    $.ajax({
        type: "post",
        async: false,
        url: "/fuwu.ashx",
        data: //发送到服务器的参数
        {
        str1: str1,
        str2: str2,
        str3: str3,
        str4: str4,
        str5: str5,
        str6: str6,
        str7: str7,
        str8: str8,
        str9: str9,
        str10: str10,
        str11: str11,
        str12: str12,
        str13: str13,
        str14: str14,
        str15: str15,
        str16: str16,
        str17: str17,
        str18: str18,
        str19: str19,
        str20: str20,
        str21: str21,
        str22: str22,
        str23: str23,
        str24: str24,
        stype: "submit",
        action: action,
        sid: sid
    },
    datatype: "json",
    success: function (result) {
        alert(result);
//        var obj = JSON.parse(result);
//        var res = obj[0];

//        if (res) {
//            if (action == "add")
//                alert("发布成功");
//            else if (action == "edit")
//                alert("更新成功");
//        } else {
//            if (action == "add")
//                alert("发布失败");
//            else if (action == "edit")
//                alert("更新失败");
//        }
    }
});
}
//绑定下拉列表
function bindSelectOptions(sel, objList) {
    $("#" + sel).empty();
    for (var i = 0; i < objList.length; i++) {
        var obj = objList[i];
        $("#" + sel).append("<option  value='" + obj.ID + "'>" + obj.NAME + "</option>");
    }
}

function bindSelectList() {
    var selectList = ["leixing", "zhuti", "quyu", "zuobiao", "touying", "quanxian", "zhouqi"];
    var selectTypeList = ["服务类型", "主题分类", "覆盖区域", "坐标系", "投影类型", "使用权限", "更新周期"];
    for (var i = 0; i < selectTypeList.length; i++) {
        var selectType = selectTypeList[i];
        var selectName = selectList[i];
        $.ajax({
            type: "post",
            async: false,
            url: "/fuwu.ashx",

            data: //发送到服务器的参数
            {
                stype: "getSubjectList",
                subtype: selectType
            },
            datatype: "json",
            success: function (result) {
                if (result) {
                    var objlist = JSON.parse(result);
                    bindSelectOptions(selectName, objlist);
                }
            },
            error: function (errorMsg) {
                // alert("请求数据失败");
            }
        });
    }
}